﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.Revit;
using HYCodeBase.WinForm;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class CreatePipeSysText : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public void MakeAll(Transaction ts, int dDis)
		{
			HYTagOperate hytagOperate = new HYTagOperate(this.comData);
			this.SetTagMaker(hytagOperate.MarkAllPipeSystemText((double)dDis));
			this.doc.Regenerate();
			ts.Commit();
		}

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException();
			}
			this.comData = commandData;
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			this.doc = activeUIDocument.Document;
			Autodesk.Revit.DB.View activeView = activeUIDocument.ActiveView;
			Selection selection = activeUIDocument.Selection;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4,
				(ViewType)117
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			if (CreatePipeSysText._hWndRevit == null)
			{
				CreatePipeSysText._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			HYTagOperate hytagOperate = new HYTagOperate(commandData);
			FormPipeSystemTag formPipeSystemTag = new FormPipeSystemTag();
			Result result;
			try
			{
				activeView = activeUIDocument.ActiveView;
				if ((int)activeView.ViewType == 4 && !(activeView as View3D).IsLocked)
				{
					YJKMessageBox.Warning("该三维视图无法标注，请锁定视角后再进行此操作！");
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					formPipeSystemTag.Show(CreatePipeSysText._hWndRevit);
					int wMsg = 7;
					CreatePipeSysText.SendMessage(CreatePipeSysText._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					for (;;)
					{
						transaction = new Transaction(this.doc, "水管标注");
						transaction.Start();
						formPipeSystemTag.SetParaentData(this, transaction);
						hytagOperate.HYLoadFamily(HYTagOperate.MarkTagType.pipe.ToString(), "HY_水管标注");
						Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeFilter(), "请选择要标注系统缩写的管道...");
						Pipe pipe = this.doc.GetElement(reference) as Pipe;
						if (!formPipeSystemTag.DataCheck())
						{
							transaction.RollBack();
						}
						else
						{
							int nLayType = formPipeSystemTag.nLayType;
							if (nLayType != 1)
							{
								if (nLayType == 2)
								{
									this.SetTagMaker(hytagOperate.MarkSamePipeSystemText(pipe, (double)formPipeSystemTag.nDistance, formPipeSystemTag.szSysName));
								}
							}
							else
							{
								this.SetTagMaker(hytagOperate.MarkSinglePipeSystemText(pipe, (double)formPipeSystemTag.nDistance, formPipeSystemTag.szSysName, formPipeSystemTag.bDeletel, reference.GlobalPoint));
							}
							this.doc.Regenerate();
							transaction.Commit();
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				formPipeSystemTag.Close();
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private void SetTagMaker(List<IndependentTag> tagList)
		{
			if (tagList == null || tagList.Count < 1)
			{
				return;
			}
			foreach (IndependentTag ele in tagList)
			{
				AssociationMarkInfo.WriteMarkInfoToElement(ele, MarkType.PipeLine);
			}
		}

		public static WindowHandle _hWndRevit;

		private ExternalCommandData comData;

		private Document doc;

		public const int USER = 1280;

		public const int WM_MYMESSAGE = 1281;
	}
}
